package aaGame;

import java.awt.Graphics2D;

import aControllerLayer.*;
import aModelLayer.Game;
import aModelLayer.Load;

/**
 * Singleton class GameEngine
 */
public class GameEngine {

	private Game gameModel;
	
	public Controller currentController;
	public TitleMenuController mTittleMenuController; 
	public RoomController mRoomController;
	public BattleController mBattleController;
	public PauseMenuController mPauseMenuController;
	public int volume;

	public GameEngine() {
		gameModel = new Game();
		
		mTittleMenuController = new TitleMenuController(gameModel.getTitleMenu(), gameModel, this);
		mRoomController = new RoomController(gameModel, this);
		mBattleController = new BattleController(gameModel, this);
		mPauseMenuController = new PauseMenuController(gameModel, this);
		
		currentController = mTittleMenuController;
		currentController.playMusic();
		printControllerName();
	}

	public void keyPressed(int key){
		currentController.keyPressed(key);
	}
	
	public void keyReleased(int key){
		currentController.keyReleased(key);
	}
	
	/** the update method with the deltaTime in seconds **/
	public void update() {
		currentController.update();
	}

	/** this will render the whole world **/
	public void render(Graphics2D g, RpgGamePanel panel) {
		currentController.getView().render(g, panel);
	}
	
	public void printControllerName(){
		System.out.println("CONTROLLER>>"+currentController.getClass().getSimpleName()+"");
		printViewName();
	}
	
	public void printViewName() {
		System.out.println("View::"+currentController.getView().getClass().getSimpleName());
	}
	
	public void startNewBattle() {
		mBattleController.startNewBattle();
		currentController = mBattleController;
		currentController.playMusic();
	}
	
	public void setController(int controller){
		switch(controller){
		case Controller.RoomController:
			currentController = mRoomController;
			break;
		case Controller.BattleController:
			currentController = mBattleController;
			break;
		case Controller.PauseMenuController:
			currentController = mPauseMenuController;
			break;
		case Controller.TitleMenuController:
			currentController = mTittleMenuController;
			break;
		}
	}
	
	public void loadGame() {
		mRoomController = new RoomController(gameModel, this);
		new Load(gameModel);
		currentController = mRoomController;
		currentController.playMusic();
	}
	
	public void startNewGame() {
		gameModel = new Game();
		gameModel.newGameStats();
		
		mTittleMenuController = new TitleMenuController(gameModel.getTitleMenu(), gameModel, this);
		mRoomController = new RoomController(gameModel, this);
		mBattleController = new BattleController(gameModel, this);
		mPauseMenuController = new PauseMenuController(gameModel, this);
		
		currentController = mRoomController;
		currentController.playMusic();
		printControllerName();
	}
}
